    
    
    function page(option){
        this.ele = document.querySelector(option.el);
        this.ele.className = "page";

        this.pageIndex = 1;
        this.create();    //方法
        this.data = option.data;
        this.methods = option.methods;
        this.bindData();
        // this.bindEvent();
    }


    // page.prototype.bindEvent = function(){

    //     let that  = this;

    //     this.prevBtn.onclick = function(){
    //         that.pageIndex--;
    //         that.bindData();
    //     }
        
    //     this.nextBtn.onclick = function(){
    //         that.pageIndex++;
    //         // console.log(that.pageIndex);
    //         that.bindData();
    //     }

    // }



    page.prototype.bindData = function(){
        let that = this
        this.allpage = Math.ceil(this.data.total/this.data.shownum);     //全部页数

        let start = 1;

        let end = this.allpage>this.data.showpage?this.data.showpage:this.allpage;

        //中间页码的位置
        this.middleNum = Math.ceil(this.data.showpage/2);

        if(this.pageIndex > this.middleNum){
            start = this.pageIndex - this.middleNum + 1;   //4-3+1

            end = this.pageIndex + this.middleNum - 1;      
        }

        if(this.pageIndex > this.allpage - this.middleNum){        //到末尾
            start = this.allpage - this.data.showpage  + 1;
            end = this.allpage;
        }

        start = start > 1?start:1;   //开头

        this.content.innerHTML = "";

        for(let i = start; i <= end; i++){     //循环添加元素
            let item = document.createElement("div");

            if(this.pageIndex == i){
                item.className = "item active";

            }else{
                item.className = "item";
            }
           
            item.innerHTML = i;


            //循环绑定点击效果
            item.onclick = function(){
                that.pageIndex = i;
                that.bindData();
                
            }

            this.content.appendChild(item);

        }



        // 做一个判断
        this.prevBtn.disabled = true;
        this.nextBtn.disabled = true;
        this.prevBtn.onclick = null;
        this.nextBtn.onclick - null;

        if(this.pageIndex > 1){

            this.prevBtn.disabled = false;

            this.prevBtn.onclick = function(){
                that.pageIndex--;
                that.bindData();
            }
        }


        if(this.pageIndex != this.allpage ){

            this.nextBtn.disabled = false;

            this.nextBtn.onclick = function(){
                that.pageIndex++;
                // console.log(that.pageIndex);
                that.bindData();
            }

        }
        
        

        if(this.methods.cb){
            this.methods.cb(this.pageIndex);
            
        }
        
    }

    
    page.prototype.create = function(){
        this.prevBtn = document.createElement("button");
        this.prevBtn.innerHTML = "上一页";
        this.ele.appendChild(this.prevBtn);

        this.content = document.createElement("div");
        this.content.className = "content"
        this.ele.appendChild(this.content);

        this.nextBtn = document.createElement("button");
        this.nextBtn.innerHTML = "下一页";
        this.ele.appendChild(this.nextBtn);

    }